package temp.lho.lws.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import temp.lho.lws.pojo.*;
import temp.lho.lws.service.IAdjustmentdetailService;
import temp.lho.lws.service.ISettlementService;
import temp.lho.lws.service.ISysUserInfoService;
import temp.lho.lws.service.UtilService;
import temp.lho.lws.vo.LayuiResult;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/claims")
public class ClaimsController {

    @Resource
    private ISettlementService settlementService;

    @Resource
    private UtilService util;

    private Logger log = Logger.getLogger(ClaimsController.class);

    /*
     *  主菜单页面跳转
     */
    @RequestMapping("/manager1")
    public String toClaims() {
        return "claims/ClaimsManagement";
    }

    @RequestMapping("/manager2")
    public String toAddClaimsPage() {
        return "claims/addClaims";
    }

    @RequestMapping("/manager3")
    public String toSetting() {
        return "claims/setting";
    }

    @RequestMapping("/togame")
    public String toGame() {
        return "claims/game";
    }

    @RequestMapping("/togame1")
    public String toGame1() {
        return "claims/game1";
    }

    @RequestMapping("/addSettlementPage")
    public String toAddClaims() {
        return "claims/addClaims";
    }

    /*
    *   副页面跳转
     */
    @RequestMapping("/settlementPage")
    public String toSettlement() {
        return "claims/settlement";
    }

    @RequestMapping("/casePage")
    public String toCase() {
        return "claims/case";
    }

    @RequestMapping("/adjustmentDetail")
    public String toAdjustmentDetail() {
        return "claims/adjustmentDetail";
    }

    /*
        获取表单数据
     */
    @RequestMapping("/getClaims")
    @ResponseBody
    public LayuiResult toClaims(ClaimsInfo claims) {
        List<SettlementInfo> se= settlementService.getSettlement(claims);
        Integer count=settlementService.getCount("settlement_info");
        if(claims.getSettlementId()!=null||claims.getPolicyNum()!=null){
            count=1;
        }
        LayuiResult lr = new LayuiResult(0,"",count,se);
        return lr;
    }

    @RequestMapping("/getAdjustment")
    @ResponseBody
    public LayuiResult toAdjustment(ClaimsInfo claims) {
        List<AdjustmentInfo> se= settlementService.getAdjustment(claims);
        Integer count=settlementService.getCount("adjustment_info");
        LayuiResult lr = new LayuiResult(0,"",count,se);
        return lr;
    }

    @RequestMapping("/getWarranty")
    @ResponseBody
    public LayuiResult toWarranty(ClaimsInfo claims) {
        List<WarrantyInfo> se= settlementService.getWarranty(claims);
        LayuiResult lr = new LayuiResult(0,"",1,se);
        return lr;
    }

    @RequestMapping("/getWarrantyId")
    @ResponseBody
    public List<Integer> toWarranty() {
        List<Integer> list= settlementService.getWarrantyId();
        return list;
    }

    @RequestMapping("/getPolicyholders")
    @ResponseBody
    public LayuiResult toPolicyholders(ClaimsInfo claims) {
        List<PolicyholdersInfo> se= settlementService.getPolicyholders(claims);
        LayuiResult lr = new LayuiResult(0,"",1,se);
        return lr;
    }

    @RequestMapping("/getRecognizee")
    @ResponseBody
    public LayuiResult toRecognizee(ClaimsInfo claims) {
        List<RecognizeeInfo> se= settlementService.getRecognizee(claims);
        LayuiResult lr = new LayuiResult(0,"",1,se);
        return lr;
    }

    @RequestMapping("/getClaimMainId")
    @ResponseBody
    public List<String> getClaimMainId() {
        List<String> list= settlementService.getClaimsMainId(null);
        return list;
    }

    @RequestMapping("/getadjustmentDetail")
    @ResponseBody
    public LayuiResult toGetadjustmentDetail(Integer adjustmentid) {
        List<Adjustmentdetail> se= settlementService.getAdjustmentDetail(adjustmentid);
        LayuiResult lr = new LayuiResult(0,"",1,se);
        return lr;
    }

    /*
        修改表单数据
     */
    @RequestMapping("/upSettlement")
    @ResponseBody
    public LayuiResult upSettlement(SettlementInfo settlement) {
        if(settlement.getSettlementAudit().equals("撤销")){
            Flowinfomation fl = new Flowinfomation();
            //赔案号，处理人ID，处理人姓名从前台获取
            List<String> list = settlementService.getClaimsMainId(settlement.getPolicyNum());
            fl.setClaimmainid(list.get(list.size()-1));     //通过保单号获取赔案号
            fl.setAssignedid("011");
            fl.setAssignedname("Niko");
            util.FlowInfomation11(fl);
        }

        int i = settlementService.upSettlement(settlement);
        LayuiResult lr = null;
        if(i>0){
            lr = new LayuiResult(1,"",1,"审核成功！");
        }else{
            lr = new LayuiResult(0,"",0,"审核失败！");
        }
        return lr;
    }

    @RequestMapping("/upCase")
    @ResponseBody
    public LayuiResult upCase(SettlementInfo settlement) {
        LayuiResult lr = null;
        int i = settlementService.upCase(settlement);
        if(i>0){
            lr = new LayuiResult(1,"",1,"结案成功！");
        }else{
            lr = new LayuiResult(0,"",0,"结案失败！");
        }
        return lr;
    }

    @RequestMapping("/getCase")
    @ResponseBody
    public LayuiResult delSettlement(ClaimsInfo claims) {
        List<ClaimMainInfo> se= settlementService.getCase(claims);

        se.stream().forEach(e -> {
            if(e.getClaimstatus().equals("1")){
                e.setClaimstatus("已报案");
            }else if(e.getClaimstatus().equals("2")){
                e.setClaimstatus("已立案");
            }else if(e.getClaimstatus().equals("3")){
                e.setClaimstatus("已结案");
            }else if(e.getClaimstatus().equals("4")){
                e.setClaimstatus("已撤案");
            }
        });

        LayuiResult lr = new LayuiResult(0,"",1,se);
        return lr;
    }

     /*
        添加表单数据
      */
    @RequestMapping("/addClaims")
    @ResponseBody
    public LayuiResult addClaims(SettlementInfo settlement) {
        LayuiResult lr = null;
        String msg = "";
        int i = 0;
        settlement.setVIN(settlementService.getFrame(settlement.getPolicyNum()));

        try{
            i = settlementService.addClaims(settlement);
            msg = "新增成功!";
        }catch(Exception e){
            msg = "新增失败!保单号重复！";
        }

        if(i>0){
            lr = new LayuiResult(1,msg,1,"1");
        }else{
            lr = new LayuiResult(0,msg,0,"0");
        }

        return lr;
    }

    @RequestMapping("/addAdjustment")
    @ResponseBody
    public LayuiResult addAdjustment(AdjustmentInfo adjustment,Adjustmentdetail adjustmentdetail) {
        LayuiResult lr = null;
        String msg = "";
        int i = 0;

        try{
            i = settlementService.addAdjustment(adjustment);
            msg = "新增成功!";
        }catch(Exception e){
            e.printStackTrace();
            msg = "新增失败!赔案号重复！";
        }
        adjustmentdetail.setAdjustmentid(settlementService.getAdjustmentId());
        adjustmentdetail.setInsurancepercentage(adjustment.getIndemnitylimit());
        Double actualpay = adjustmentdetail.getDeductibleaverage()*Double.parseDouble(adjustmentdetail.getAbsolutelyfranchise())-adjustmentdetail.getSuecharges();
        adjustmentdetail.setActualpay(actualpay);
        if(actualpay>adjustmentdetail.getIndemnitylimit()){
            actualpay = adjustmentdetail.getIndemnitylimit();
        }
        i = settlementService.addAdjustmentdetail(adjustmentdetail);

        if(i>0){
            lr = new LayuiResult(1,msg,1,"");
        }else{
            lr = new LayuiResult(0,msg,0,"");
        }

        return lr;
    }

    /*
    *     删除请求
    */
    @RequestMapping("/delSettlement")
    @ResponseBody
    public LayuiResult delSettlement(SettlementInfo settlement) {
        int i = settlementService.delSettlement(settlement);
        LayuiResult lr = null;
        if(i>0){
            lr = new LayuiResult(1,"",1,"删除成功！");
        }else{
            lr = new LayuiResult(0,"",0,"删除失败！");
        }
        return lr;
    }

    @RequestMapping("/delAdjustment")
    @ResponseBody
    public LayuiResult delAdjustment(Integer adjustmentid) {
        int i = settlementService.delAdjustment(adjustmentid);
        int j = settlementService.delAdjustmentDetail(adjustmentid);
        LayuiResult lr = null;
        if(i>0&&j>0){
            lr = new LayuiResult(1,"",1,"删除成功！");
        }else{
            lr = new LayuiResult(0,"",0,"删除失败！");
        }
        return lr;
    }

}
